package com.rcp.server;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rcp.objects.Order;

@WebServlet("/Finalize")
public class Finalize extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public Finalize() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String url = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
        String username = "cs437group01";
        String password = "VHj#dj!v";
        
        HttpSession session = request.getSession(true);
        session = request.getSession();
		ArrayList<Order> totalOrder = (ArrayList<Order>) session.getAttribute("totalOrder");
        float totalPrice = (float) 0.0;//(Float) session.getAttribute("totalPrice");
		
        
        Connection c;
		try {
			c = DriverManager
			    .getConnection( url, username, password );
//getting CCard info	
			Statement stmt = c.createStatement();
	        ResultSet rs = stmt.executeQuery("SELECT * FROM  `CreditCards` ");
	        int cCard = 0;
	        
        	while( rs.next() )
            {	            		
        		cCard = rs.getInt("CCNumber");
            }
        	
        	cCard = cCard % 10000;
//removing entree        	
        	if(request.getParameter("remove") != null){
        		totalOrder.remove( Integer.parseInt(request.getParameter("remove")) );
        	}
//updating total price        	
        	for(int i = 0; i < totalOrder.size(); i++){
        		totalPrice += totalOrder.get(i).getPrice();
        	}

        	
//calculating side
        	int sideCounter = 0;
        	float addSides = 0;
        	
        	for(int i = 0; i < totalOrder.size(); i++){
        		for(int j = 0; j < totalOrder.get(i).getSides().length; j++){
            		sideCounter++;
            	}
        	}
        	addSides = (float) ((sideCounter - totalOrder.size() - 0.01) * 2);
        	totalPrice = totalPrice + addSides;
        	
//inserting order history        	
//        	stmt.executeUpdate("INSERT INTO `Orders`(`CustomerID`, `EmployeeID`, `Cost`) VALUES (2, 2, " + totalPrice+")");
//        	
//        	for(int i = 0; i < totalOrder.size(); i++){
//        		if(totalOrder.get(i).getEntree().equals("Black Pepper Chicken") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 1)");
//        		else if(totalOrder.get(i).getEntree().equals("Fried Chicken") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 2)");
//        		else if(totalOrder.get(i).getEntree().equals("Cheese Burger") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 3)");
//        		else if(totalOrder.get(i).getEntree().equals("T-Bone Steak") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 4)");
//        		else if(totalOrder.get(i).getEntree().equals("Pork Chops") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 5)");
//        		else if(totalOrder.get(i).getEntree().equals("Veggie Burger") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 8)");
//        		else if(totalOrder.get(i).getEntree().equals("Spinach and Feta Pasta") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 9)");
//        		else if(totalOrder.get(i).getEntree().equals("Carnitas") )
//        			stmt.executeUpdate("INSERT INTO `OrderToEntrees`(`OrderID`, `EntreeID`) VALUES (2, 10)");
//        	}
        	
        	c.close();
        	session.setAttribute("totalPrice", totalPrice);
        	getServletContext().setAttribute( "cCard", cCard );
        	getServletContext().setAttribute( "addSides", addSides );
        	request.getRequestDispatcher("/WEB-INF/server/FinalizeView.jsp").forward(request, response);
        	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
